/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.controle.ejb;

import br.com.controle.entity.Medida;
import br.com.controle.entity.TipoEndereco;
import java.io.Serializable;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.annotation.Resource;
import javax.ejb.Stateless;
import javax.ejb.LocalBean;

import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageProducer;
import javax.jms.ObjectMessage;
import javax.jms.Queue;
import javax.jms.Session;
import javax.jms.TextMessage;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
 
@Stateless
@LocalBean
public class MedidaSessionBean {
    @Resource(mappedName = "jms/NotificationQueue")
    private Queue notificationQueue;
    @Resource(mappedName = "jms/NotificationQueueFactory")
    private ConnectionFactory notificationQueueFactory;

    @PersistenceContext
    private EntityManager em;    

    public List<Medida> retrieve() {
        Query query = em.createNamedQuery("Medida.findAll");
        return query.getResultList();
    }
    
    public Medida getMedida(Medida medida){
        Query query = em.createNamedQuery("Medida.findById").setParameter("id", medida.getId());
        return (Medida) query.getSingleResult();
    }    
    
    public void delete(Medida medida) {
        Medida excluir = em.find(Medida.class, medida.getId());
        em.remove(excluir);
    }    
        
     public Medida create(Medida medida)
     {
        em.merge(medida); 
        try
        {
            sendJMSMessageToNotificationQueue(medida);
        }
        catch (JMSException ex)
        {
            System.out.println("Update error:" + ex.getMessage());
            Logger.getLogger(MedidaSessionBean.class.getName()).log(Level.SEVERE, null, ex);
            ex.printStackTrace();
        }        
        catch (Exception e)
        {
            System.out.println("Update error:" + e.getMessage());
            Logger.getLogger(MedidaSessionBean.class.getName()).log(Level.SEVERE, null, e);
            e.printStackTrace();
        }
        System.out.println("Medida criada");
        return medida;
    }    

    public Medida update(Medida medida)
    {
           
        Medida updated = em.merge(medida); 
        try
        {
            sendJMSMessageToNotificationQueue(updated);
        }
        catch (JMSException ex)
        {
            System.out.println("Update error:" + ex.getMessage());
            Logger.getLogger(MedidaSessionBean.class.getName()).log(Level.SEVERE, null, ex);
            ex.printStackTrace();
        }        
        catch (Exception e)
        {
            System.out.println("Update error:" + e.getMessage());
            Logger.getLogger(MedidaSessionBean.class.getName()).log(Level.SEVERE, null, e);
            e.printStackTrace();
        }
        System.out.println("Medida atualizada no bean");
        return updated;
    }

     private Message createJMSMessageForjmsNotificationQueue(Session session, Object messageData) throws JMSException
    {
        //Modified to use ObjectMessage instead
        ObjectMessage tm = session.createObjectMessage();
        tm.setObject((Serializable) messageData);
        return tm;
    }

    private void sendJMSMessageToNotificationQueue(Object messageData) throws JMSException{
        Connection connection = null;
        Session session = null;
        try{
            connection = notificationQueueFactory.createConnection();
            session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
            MessageProducer messageProducer = session.createProducer(notificationQueue);
            messageProducer.send(createJMSMessageForjmsNotificationQueue(session, messageData));
        }
        finally{
            if (session != null){
                try{
                    session.close();
                }
                catch (JMSException e){
                    Logger.getLogger(this.getClass().getName()).log(Level.WARNING, "Cannot close session", e);
                }
            }
            if (connection != null){
                connection.close();
            }
        }
    }    
}
